home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Alpha ƒ / Tcl / SystemCode / latexSmart.tcl < prev    next >
Text File  |  1996-01-15  |  4KB  |  178 lines

  1. #############################################################################
  2. #############################################################################
  3. #
  4. # latexSmart.tcl (called from latex.tcl)
  5. #
  6. # Smart quotes, dots, subscripts, and superscripts
  7. #
  8. #############################################################################
  9. #
  10. # Original author unknown
  11. #
  12. # Maintainer:  Tom Scavo <trscavo@syr.edu>
  13. #
  14. #############################################################################
  15. #############################################################################
  16.  
  17. #--------------------------------------------------------------------------
  18. # Smart quotes:
  19. #--------------------------------------------------------------------------
  20.  
  21. proc smartDQuote {} {
  22.     global TeXmodeVars
  23.     if {[isSelection]} { deleteSelection }
  24.     if { !$TeXmodeVars(smartQuotes) || [literalChar] } { insertText {"}; return }
  25.     if {[leftQ]} {
  26.         insertText {``}
  27.     } else {
  28.         insertText {''}
  29.     }
  30. }
  31.  
  32. # bind double quote:
  33. ascii 0x22 <s> smartDQuote TeX
  34.  
  35. proc smartQuote {} {
  36.     global TeXmodeVars
  37.     if {[isSelection]} { deleteSelection }
  38.     if { !$TeXmodeVars(smartQuotes) || [literalChar]  } { insertText {'}; return }
  39.     if {[leftQ]} {
  40.         insertText {`}
  41.     } else {
  42.         insertText {'}
  43.     }
  44. }
  45.  
  46. # bind single quote:
  47. ascii 0x27 smartQuote TeX
  48.  
  49. proc leftQ {} {
  50.     if { [getPos] == 0 } { return 1 };
  51.     set q [lookAt [expr [getPos]-1]]
  52.     case $q in {
  53.         {\t}  {return 1}
  54.         {(}  {return 1}
  55.         {\{}  {return 1}
  56.         {[}  {return 1}
  57.         {<}  {return 1}
  58.         {\ } {return 1}
  59.         {\r} {return 1}
  60.     }
  61.     return 0
  62. }
  63.  
  64. #--------------------------------------------------------------------------
  65. # Smart dots:
  66. #--------------------------------------------------------------------------
  67.  
  68. proc smartDots {} {
  69.     global TeXmodeVars
  70.     if {[isSelection]} { deleteSelection }
  71.     if { !$TeXmodeVars(smartDots) || [literalChar] } { insertText {.}; return }
  72.     if {[lookAt [expr [set endPos [getPos]]-1]] == "."} then {
  73.         if {[lookAt [set begPos [expr $endPos-2]]] == "."} then {
  74.             replaceText $begPos $endPos "\\ldots"
  75.         } else {
  76.             insertText "."
  77.         }
  78.     } else {
  79.         insertText "."
  80.     }
  81. }
  82.  
  83. # bind period:
  84. ascii 0x2e smartDots "TeX"
  85.  
  86. #--------------------------------------------------------------------------
  87. # Smart subscripts and superscripts:
  88. #--------------------------------------------------------------------------
  89.  
  90. proc smartSubscripts {} {
  91.     smartScripts {_}
  92. }
  93.  
  94. proc smartSuperscripts {} {
  95.     smartScripts {^}
  96. }
  97.  
  98. proc smartScripts {char} {
  99.     global TeXmodeVars
  100.     if {[isSelection]} { deleteSelection }
  101.     if { !$TeXmodeVars(smartScripts) || [literalChar] } then {
  102.         insertText $char
  103.         return
  104.     }
  105.     if { $TeXmodeVars(smartScripts) } then {
  106.         if { $char == {_} } {subscript} {superscript}
  107.     }
  108. }
  109.  
  110. # bind underscore and caret:
  111. ascii 0x5f <s> smartSubscripts "TeX"
  112. ascii 0x5e <s> smartSuperscripts "TeX"
  113.  
  114. #--------------------------------------------------------------------------
  115. # Escapes and exceptions:
  116. #--------------------------------------------------------------------------
  117.  
  118. # proc literalChar {} {
  119. #     return [expr {[lookAt [expr [getPos] - 1]] == "\\"}]
  120. # }
  121. proc escapeSmartStuff {} {
  122.     if {![isSelection]} then {
  123.         set pos [getPos]
  124.         # Escape double quotes:
  125.         if { $pos > 1 } then {
  126.             set pos2 [expr $pos - 2]
  127.             if { [getText $pos2 $pos] == "''" } then {
  128.                 replaceText $pos2 $pos {"}
  129.                 return
  130.             } elseif { [getText $pos2 $pos] == "``" } then {
  131.                 replaceText $pos2 $pos {"}
  132.                 return
  133.             }
  134.         }
  135.         # Escape single quote:
  136.         if { $pos > 0 } then {
  137.             set pos1 [expr $pos - 1]
  138.             if { [lookAt $pos1] == "`" } then {
  139.                 backSpace
  140.                 insertText {'}
  141.                 return
  142.             }
  143.         }
  144.         # Escape dots:
  145.         if { $pos > 5 } then {
  146.             set pos6 [expr $pos - 6]
  147.             if { [getText $pos6 $pos] == "\\ldots" } then {
  148.                 replaceText $pos6 $pos {...}
  149.                 return
  150.             }
  151.         }
  152.         # Escape underscore:
  153.         if { $pos > 1 && [maxPos] > [expr $pos + 1] } then {
  154.             set begPos [expr $pos - 2]
  155.             set endPos [expr $pos + 2]
  156.             if { [getText $begPos $endPos] == "_\{\}Ñ" } then {
  157.                 replaceText $begPos $endPos {_}
  158.                 return
  159.             }
  160.         }
  161.         # Escape caret:
  162.         if { $pos > 1 && [maxPos] > [expr $pos + 1] } then {
  163.             set begPos [expr $pos - 2]
  164.             set endPos [expr $pos + 2]
  165.             if { [getText $begPos $endPos] == "^\{\}Ñ" } then {
  166.                 replaceText $begPos $endPos {^}
  167.                 return
  168.             }
  169.         }
  170.     }
  171.     backSpace
  172. }
  173.  
  174. # bind delete key:
  175. ascii 0x08 escapeSmartStuff "TeX"
  176.  
  177.